
var token = "&hpLogin_token=" + localStorage.getItem("hpLogin_token");

(function(){
  var $dig = $(".coverDig");
  //编辑客户信息
  $("#clientDetail").on("click","#editClient",function(){
    $("#editClientDig").show().siblings().hide();
    $dig.show();
  });
  // 发送编辑客户信息
  $("#editBaseInfoBtn").on("click",function(e){
    e.stopPropagation();
    var isphone = $("#editBaseInfo").find("input[name='mobile']");
    if(checkForm(isphone)==false) return;
    var id = parseInt(GetUrlString("id"));
    var data = $("#editBaseInfo").serialize() + token +"&id=" +id;
    $.ajax({
        type:'get',
        url:address+'/customer/edit.do',
        data:data,
        dataType:'json',
        success:function(result){
          if(result.status ==200){
            message(result.message);
            setTimeout(function(){
              $dig.hide();
              recDetail.getCustomerDetail();
            },2000);
          }
        },
        error:function(result) {
          message(result.message);
        }
    })
  });

  $("#flow").on("click",".flow span",function(){
    var index = $(this).index();
    $(".flowTab").find(".item").eq(index-1).show().siblings().hide();
  });

  //身份证、银行卡弹窗
  $("#myCard").on("click","i",function(){
    var $this = $(this);
    if($this.hasClass("icon-idCard")){
      $("#idCardDig").show().siblings().hide();
    }else{
      $("#bankCardDig").show().siblings().hide();
    }
    $dig.show();
  });

})();

function Recommend(){
  this.pageSize = 10;
  this.pageNum = 1;
  this.hpWrite_token = localStorage.getItem("hpWrite_token");
  this.hpLogin_token = localStorage.getItem("hpLogin_token");
}

Recommend.prototype.getCustomer = function(keywords){
  var keywords = keywords || "";
  $.ajax({
      type:'POST',
      url:address+'/customer/list.do',
      data:{
        "keywords":keywords,
        "pageNum":this.pageNum,
        "pageSize":this.pageSize,
        "hpWrite_token":this.hpWrite_token,
        "hpLogin_token":this.hpLogin_token
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          $("#allClient").html(customerTPL(result.data));
        }else{
          var html = '<div class="nodata">没有数据</div>';
          $("#allClient").html(html);
        }
      },
      error:function(result){
        message(result.message)
      }
  })
}

Recommend.prototype.getCustomerDetail = function(){
  var id = parseInt(GetUrlString("id"));
  $.ajax({
      type:'POST',
      url:address+'/customer/get.do?format=true',
      data:{
        "id":id,
        "hpLogin_token":this.hpLogin_token
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
            var info = result.data.customer;
            $("#baseInfo").html(template("baseInfoTPL",info));
            $("#editBaseInfo").html(template("editBaseInfoTPL",info));
            $("#flow").append(template("flowTPL",info));
        }
      },
      error:function(result){
        message(result.message)
      }
  })
}

// 客户列表模板
function customerTPL(data) {
  var data = data,html ="";
  $.each(data.list,function(index,info){
      html += '<div class="p10 b-b dbox m-b5">'+
        '<div class="flex1">'+
          '<p><span class="fr gray">'+info.recordTimeStr+'</span>'+info.name+'</p>'+
          '<p class="dbox"><span>'+info.mobile+'</span><span class="t-c">'+info.newBuildings+'</span><span class="t-r orange">'+info.stateStr+'</span></p>'+
        '</div>'+
        '<div class="detail "><a href="sell-recommend-detail.html?id='+info.id+'" class="dib"><i class="icon-more-right">&nbsp;</i></a></div>'+
      '</div>';
  })
  return html;
}
// 添加客户
$("#addMan").on("click",function(){
    var pass = true;
    var checkinput = $("#addManForm").find("input[type='text']").not("#tel");
    if(!checkNull(checkinput,"请完善客户信息")) return;

    checkinput.each(function(){
        if(!checkForm($(this))){
          pass = false;
          return false;
        }
    });
    if(!pass) return false;

    var data = $("#addManForm").serialize() + token;
    $.ajax({
        type:'POST',
        url:address+'/customer/add.do',
        data:data,
        dataType:'json',
        success:function(result){
          message(result.message);
          if(result.status==200){
            window.location.href="sell-recommend.html";
          }
        }
    })
})

$(".describe").on("keyup",function(){
  var num = $(this)
})

// 获取楼盘名称列表插件
;(function($){
    $.extend($.fn,{
        getHouseNameList:function(data){
          var cityName = localStorage.getItem("cityName"), //城市名
              keywords = data.keywords || "", //关键词
              mbName = data.mbName,  //名字目标
              mbid = data.mbid,     //id目标
              $this = $(this);
          $.ajax({
              type:'GET',
              url:address+'/newBuildings/listNewBuildingNames.do?format=true',
              data:{
                "city":cityName,
                "keywords":keywords
              },
              dataType:'json',
              success:function(result){
                if(result.status ==200){
                  var html = "";
                  $.each(result.data.list,function(index,info){
                      html += '<li data-id="'+info.id+'">'+info.name+'</li>'
                  })
                  $("#houseNameList").html(html);
                }
              }
          })
          $this.on("click",function(){
            $(".digList").show();
          })
          $("#houseNameList").on("click","li",function(){
              var houseName = $(this).text(),
                  houseNameId = $(this).attr("data-id");
              $('.'+mbid).val(houseNameId);
              $('.'+mbName).text(houseName);
              $(".digList").hide();
          })
          return this;
        }

    })
})(Zepto||JQuery);


